﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home_UserManage : System.Web.UI.Page
{
    public LanguageManage Language;
    private UserEntity currentUser;
    public List<UserEntity> users { get; set; }
    private UserAdapter userAdap;
    private RoleAdapter roleAdapter;
    AuthenAdapter authAdapter;
    public bool isEdit = false;
    public string userName;
    public string title = "Createuser";
    public UserEntity userSelected = new UserEntity();
    public string deletecf = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check Login
        currentUser = UserManager.Instance.Getuser();
        if (!currentUser.isLogin)
            Response.Redirect("~/Login.aspx");

        Language = new LanguageManage();
        userAdap = new UserAdapter();
        roleAdapter = new RoleAdapter();
        authAdapter = new AuthenAdapter();
        btn_create.Text = Language["Createuser"];
        users = userAdap.getAllUser();
        Session["menu_id"] = "usermanage";
        deletecf = Language["msgdeletebook"];
        if (!IsPostBack)
        {
            comboRole.DataSource = roleAdapter.getAllRoles();
            comboRole.DataTextField = "RoleName";
            comboRole.DataValueField = "ID";
            comboRole.DataBind();
        }

        if (Request.QueryString["EditUser"] != null)
        {
            userName = Request.QueryString["EditUser"];
            userSelected = userAdap.getUserbyName(userName);
            comboRole.Items.FindByValue(userSelected.RoleID.ToString()).Selected = true;
            btn_create.Text = Language["Update"];
            title = "Edituser";
            isEdit = true;
        }

        
    }
    public string GetRoleById(long roleId)
    {
        return roleAdapter.getRoleById(roleId).RoleName;
    }
    public string ConvertActive(bool active)
    {
        if (active == true)
            return "checked";
        else
            return "";
    }
    protected void btn_create_Click(object sender, EventArgs e)
    {
        try
        {


            if (Request.QueryString["EditUser"] == null)
            {
              
                userSelected.Account = Request.Form["txtName"].ToString();
                userSelected.Password = authAdapter.Encrypt(Request.Form["txtPass"].ToString(), true);
                userSelected.Token = authAdapter.Encrypt(Request.Form["txtName"].ToString() + Request.Form["txtPass"].ToString(), true);
                userSelected.RoleID = Convert.ToInt32(comboRole.SelectedValue.ToString());
                userSelected.Active = false;
                userSelected.DateCreate = DateTime.Now;
                userSelected.Mail = Request.Form["txtMail"].ToString();

                if (userSelected.Account == "" || userSelected.Password == "" || userSelected.Token == "")
                {
                    string mess = "<script type=\"text/javascript\">" + "alert('" + Language["ErrInput"] + "');</" + "script>";
                    RegisterStartupScript("PopupScript", mess);
                }
                else
                {
                    userAdap.CreateUser(userSelected);
                }

            }
            else
            {
                if (!Request.Form["txtPass"].ToString().Equals("") && !Request.Form["txtName"].ToString().Equals(""))
                {
                    userSelected.Password = authAdapter.Encrypt(Request.Form["txtPass"].ToString(), true);
                    userSelected.Token = authAdapter.Encrypt(Request.Form["txtName"].ToString() + Request.Form["txtPass"].ToString(), true);
                }
                userSelected.Mail = Request.Form["txtMail"].ToString();
                //comboRole.Items.FindByValue(userSelected.RoleID.ToString()).Selected = false;
                userSelected.RoleID = Convert.ToInt32(comboRole.SelectedValue.ToString());
                userAdap.UpdateUser(userSelected);

            }
        }
        catch (Exception)
        {

            throw;
        }

        Response.Redirect("~/Home/UserManage.aspx");
    }
}